package system

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
	"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
	"go.uber.org/zap"
	"new_club_recruitment/global"
	"new_club_recruitment/model/common/request"
	"new_club_recruitment/model/common/response"
)

type WechatPay struct{}

// CloseOrder 关闭订单
func (WechatPay *WechatPay) CloseOrder(c *gin.Context) {
	var info request.WechatPayId
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if code, err := wechatPaySerice.CloseOrder(&info.TransactionId, &info.Mchid); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(code, "创建成功", c)
	}
}

// Prepay 预付款
func (WechatPay *WechatPay) Prepay(c *gin.Context) {
	var info jsapi.PrepayRequest
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if resp, code, err := wechatPaySerice.Prepay(info); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(gin.H{
			"Code": code,
			"res":  resp,
		}, "创建成功", c)
	}
}

// QueryOrderById 通过id查找订单
func (WechatPay *WechatPay) QueryOrderById(c *gin.Context) {
	var info request.WechatPayId
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if resp, code, err := wechatPaySerice.QueryOrderById(&info.TransactionId, &info.Mchid); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(gin.H{
			"Code": code,
			"res":  resp,
		}, "创建成功", c)
	}
}

// QueryOrderByOutTradeNo 出单号查询订单
func (WechatPay *WechatPay) QueryOrderByOutTradeNo(c *gin.Context) {
	var info request.WechatPayId
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if resp, code, err := wechatPaySerice.QueryOrderByOutTradeNo(&info.TransactionId, &info.Mchid); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(gin.H{
			"Code": code,
			"res":  resp,
		}, "创建成功", c)
	}
}

// Create 创建退款
func (WechatPay *WechatPay) Create(c *gin.Context) {
	var info refunddomestic.CreateRequest
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if resp, code, err := wechatPaySerice.Create(info); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(gin.H{
			"Code": code,
			"res":  resp,
		}, "创建成功", c)
	}
}

// QueryByOutRefundNo 查询退票否
func (WechatPay *WechatPay) QueryByOutRefundNo(c *gin.Context) {
	var info request.WechatPayId
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if resp, code, err := wechatPaySerice.QueryByOutRefundNo(&info.TransactionId, &info.Mchid); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(gin.H{
			"Code": code,
			"res":  resp,
		}, "创建成功", c)
	}
}
